package service

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	cacheNew "github.com/patrickmn/go-cache"
	"github.com/tycme/gin-chat/helper"
	"github.com/tycme/gin-chat/models"
	"strconv"
	"sync"
	"text/template"
	"time"
)

type Report struct {
	flowLock  sync.Mutex
	loginLock sync.Mutex
}

var report Report
var cacheGo = cacheNew.New(1*time.Minute, 1*time.Minute)

func IncrmentFlow(flowNum int) {

	report.flowLock.Lock()

	currentTime := time.Now().Format("2006-01-02 15:04:05")
	// 存储缓存项
	key := "flow:" + currentTime
	//	fmt.Println("flowkey:" + key)
	sendkey := "send:" + currentTime
	_, found := cacheGo.Get(key)

	if found {
		//		fmt.Println("flowkey:" + key + "value:" + strconv.Itoa(value.(int)))
		cacheGo.IncrementInt(key, flowNum)
		cacheGo.IncrementInt(sendkey, 1)
	} else {
		cacheGo.Set(sendkey, 1, cacheNew.DefaultExpiration)
		cacheGo.Set(key, flowNum, cacheNew.DefaultExpiration)
	}
	report.flowLock.Unlock()
}
func IncrmentReport() {
	report.loginLock.Lock()

	currentTime := time.Now().Format("2006-01-02 15:04:05")
	// 存储缓存项
	loginKey := "login:" + currentTime

	//fmt.Println("loginkey:" + key)
	_, found := cacheGo.Get(loginKey)

	if found {
		//	fmt.Println("loginkey:" + key + "value:" + strconv.Itoa(value.(int)))
		cacheGo.IncrementInt(loginKey, 1)

	} else {

		cacheGo.Set(loginKey, 1, cacheNew.DefaultExpiration)
	}
	report.loginLock.Unlock()
}

type reportResult struct {
	flow string `json:"flow"`

	login  string `json:"login"`
	client [60]lineReport
}
type lineReport struct {
	Key   string `json:"Key"`
	Value int    `json:"Value"`
}

func GetReport(c *gin.Context) {
	now := time.Now()
	// 获取一分钟之前的时间
	reportType := c.Query("reportType") // 自己的id
	var lineString [60]lineReport
	for a := 0; a < 60; a++ {

		st, _ := time.ParseDuration("-" + strconv.Itoa(a) + "s")
		oneMinuteAgo := now.Add(st)
		//	fmt.Printf("a 的值为: %d\n", oneSecondAfter)
		key := oneMinuteAgo.Format("2006-01-02 15:04:05")
		val, found := cacheGo.Get(reportType + key)
		fmt.Println(reportType + key)
		if found {
			//lineString = append(lineString, lineReport{Key: key, Value: val.(int)})
			//lineReportList[a] = lineReport{Key: key, Value: val.(int)}
			//myList.PushBack(lineReport{Key: key, Value: val.(int)})
			//	loginArray[a] = val.(int)
			lineString[a] = lineReport{Key: key, Value: val.(int)}
			fmt.Println("login:" + key + "    " + strconv.Itoa(val.(int)))
		} else {

		}
	}

	// 使用Items()函数获取所有缓存项
	items := cacheGo.Items()

	// 遍历缓存项
	for key, value := range items {
		fmt.Printf("Key: %s, Value: %v, Expiration: %v\n", key, value.Object, value.Expiration)
	}

	jsonList1, err := json.Marshal(lineString)
	if err != nil {
		fmt.Printf("JSON marshaling failed: %s", err)
	}
	fmt.Printf("%s\n", jsonList1)
	//reportRes := reportResult{login: string(jsonList1)}
	c.JSON(200, lineString)
}

func GetClientLength(c *gin.Context) {
	var count int
	Manager.Clients.Range(func(key, value interface{}) bool {
		count++
		return true
	})
	c.JSON(200, count)
}
func ClientDemo(c *gin.Context) {
	ind, err := template.ParseFiles("views/client.html")
	if err != nil {
		panic(err)
	}
	clientId := c.GetHeader("clientId")
	if clientId == "" {
		clientId, _ = c.GetQuery("clientId")
	}
	if clientId == "" {
		clientId = "18"
	}
	token, _ := models.RDB.Get(c, clientId).Result()
	type Data struct {
		Token  string
		UserId string
	}
	userClaims, err := helper.AnalyseToken(token)
	ind.Execute(c.Writer, &Data{
		Token:  token,
		UserId: userClaims.Identity,
	})
}
